import pygame
from pygame.locals import *

from random import randint, choice, shuffle

class PuzzlePieceGroup(object):
    def __init__(self,piece):
        self.pieces = [piece]

    def is_piece(self):
        return False

    def append(self,piece):
        if piece.is_piece():
            self.pieces.append(piece)
            op = self.pieces[0]
            p = piece
            p.rect.x = op.rect.x+((p.px-op.px)*64)
            p.rect.y = op.rect.y+((p.py-op.py)*64)
        else:
            for p in piece.pieces:
                self.append(p)

    def is_friend(self,piece):   
        if piece.is_piece():     
            for p in self.pieces:
                if p.is_friend(piece):
                    return True
            return False
        else:
            for p in piece.pieces:
                if self.is_friend(p):
                    return True
            return False
    
    def is_hit(self,pos):
        for p in self.pieces:
            if p.is_hit(pos):
                return True
        return False
    
    def translate(self,rel):
        for p in self.pieces:
            p.translate(rel)

    def draw(self,dest):
        for p in self.pieces:
            p.draw(dest)

    def draw_shadow(self,dest,pos=3):
        for p in self.pieces:
            p.draw_shadow(dest,pos)



class PuzzlePiece(object):

    def __init__(self,img,points):

        #self.finalx = px
        #self.finaly = py
        self.correct = False
        self.points = points
        self.posx = 0
        self.posy = 0
        self.img = img
        self.rect = self.img.get_rect()
        self.simg = img
        self.srect = self.img.get_rect()
        
        self.srect.center = (580+3,150+3)

    def is_piece(self):
        print "puzzle Is piece"
        return True
            
    def is_hit(self,pos):        
        if self.rect.collidepoint(pos):
            x = pos[0]-self.rect.left
            y = pos[1]-self.rect.top
            col = self.img.get_at((x, y))
            
            if col[3] == 255:
                print "Puzzle is hit"
                return True
        return False      
    
    def set_pos(self,posx,posy):
        self.rect.x = posx
        self.rect.y = posy       
        print "Puzzle set pos"

    def translate(self,rel):
        self.rect.x+=rel[0]
        self.rect.y+=rel[1]
        print "Puzzle translate"
        
    def rigth_position(self):
        print "Validando la posicion"
        for i in range (len(self.points)): 
            point = self.points[i]
            if self.rect.collidepoint(self.points[i]):
                print "Retornando true "
                self.rect.center = self.points[i]
                self.correct = True
                return True
        self.correct = False
        return False

    def draw(self,dest):        
        #dest.blit(self.img,self.rect.topleft)      
        print "Hola mundo"
        
    def draw_shadow(self,dest,pos=30):
        self.srect.x = self.rect.x+pos
        self.srect.y = self.rect.y+pos
        #dest.blit(self.simg,self.srect.topleft)    

PEG = 0
HOLE = 1
EDGE = 2

TOP = 0
RIGHT = 1
BOTTOM = 2
LEFT = 3

class PuzzleFactory(object):

    def __init__(self,image,mask_image,bevel_image,shadow_image):
        self.img = (pygame.Surface((640+32, 512+32), pygame.SRCALPHA)).convert_alpha()
        self.img.fill((0,0,0,0))
        #self.img.blit(image,(16,16))

        self.mask_img = mask_image
        self.bevel_img = bevel_image
        self.shadow_img = shadow_image
    
        self._setup_edges()

    def _setup_edges(self):
        self.edges = []
        for y in range(8):
            row = []
            self.edges.append(row)
            for x in range(10):
                # set top
                if y == 0:
                    top = EDGE
                else:
                    if self.edges[y-1][x][BOTTOM]==PEG:
                        top = HOLE
                    else:
                        top = PEG
                # set bottom
                if y == 7:
                    bottom = EDGE
                else:
                    bottom = choice((PEG,HOLE))
                # set left
                if x == 0:
                    left = EDGE
                else:
                    if self.edges[y][x-1][RIGHT]==PEG:
                        left = HOLE
                    else:
                        left = PEG
                # set right
                if x == 9:
                    right = EDGE
                else:
                    right = choice((PEG,HOLE))
                row.append((top,right,bottom,left))    

    def _make_form(self,img,edges):

        top,right,bottom,left=edges

        res = (pygame.Surface((96, 96), pygame.SRCALPHA)).convert_alpha()
        
        res.fill((0,0,0,0))

        center = pygame.Rect(32,32,32,32)
        top_r = pygame.Rect(16+96*top,0,64,32)
        bottom_r = pygame.Rect(16+96*bottom,64,64,32)
        right_r = pygame.Rect(64+96*right,32,32,32)
        left_r = pygame.Rect(0+96*left,32,32,32)

        #res.blit(img,center,center)
        #res.blit(img,(16,0),top_r)
        #res.blit(img,(16,64),bottom_r)
        #res.blit(img,(0,32),left_r)
        #res.blit(img,(64,32),right_r)
        return res

    def _make_pieces(self,px,py):
        r = pygame.Rect(px*64,py*64,100,60)
        src = [] 
        
        for img in pieces_img:
            dest = self.img.subsurface(r)
            #dest.blit(bevel, (0,0))
            PuzzlePiece(dest,40+64*px,40+64*py,px,py)
            src.append()

        #mask = self._make_form(self.mask_img,self.edges[py][px])
        #bevel = self._make_form(self.bevel_img,self.edges[py][px])
        #shadow = self._make_form(self.shadow_img,self.edges[py][px])

        #dest = (pygame.Surface((96, 96), pygame.SRCALPHA)).convert_alpha()
        dest = (pygame.Surface((100, 60), pygame.SRCALPHA)).convert_alpha()
        dest.fill((0,0,0,0))

        for y in range(100):
            for x in range(60):              
                m = mask.get_at((x,y))
                if m[3] == 255:
                    dest.set_at((x,y),src.get_at((x,y)))    

        #dest.blit(bevel,(0,0))
        
        return PuzzlePiece(src,shadow,40+64*px,40+64*py,px,py)

    def get_pieces(self):
        p = []
        for y in range(8):
            for x in range(10):
                p.append(self._make_piece(x,y))
        shuffle(p)
        return p
